package com.tanhua.server.controller;


import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.TodayBest;
import com.tanhua.server.service.MovementsService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.GET;
import java.io.IOException;
import java.util.List;

@RequestMapping("/movements")
@RestController
public class MovementsController {

    @Autowired
    private MovementsService movementsService;


    /*
     *  谁看过我
     *    请求方式和请求链接 : GET  /visitors
     *    请求参数:
     *    返回值: List<TodayBest>
     * */

    @GetMapping("/visitors")
    public ResponseEntity visitors() {
       List<TodayBest>  list=movementsService.visitors();

        return ResponseEntity.ok(list);

    }


    /*
     * 动态-发布
     *    请求方式和请求链接 :POST
     *    请求参数: Body:  textContent	T文本,imageContent	文件
     *    返回值: 无
     * */
    @PostMapping
    public ResponseEntity movements(String textContent, MultipartFile[] imageContent) throws IOException {

        movementsService.movements(textContent, imageContent);

        return ResponseEntity.ok(null);
    }

    /*
     * 查询我的动态
     *    请求方式和请求链接 :GET  /all
     *    请求参数: Query:
     *            page,pagesize,userId
     *    返回值: PageResult (里面封装了 MovementsVo)
     * */
    @GetMapping("/all")
    public ResponseEntity all(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pagesize,
                              Long userId
    ) {
        PageResult result = movementsService.findByUserId(page, pagesize, userId);

        return ResponseEntity.ok(result);


    }

    /*
     * 查询好友动态
     *    请求方式和请求链接 : GET
     *    请求参数:Query: page,pagesize
     *    返回值:PageResult (里面封装了 MovementsVo)
     * */
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {

        PageResult result = movementsService.findByFriendId(page, pagesize);
        return ResponseEntity.ok(result);

    }

    /*
     * 查询推荐动态
     *    请求方式和请求链接 : GET   /recommend
     *    请求参数:Query: page,pagesize
     *    返回值:PageResult (里面封装了 MovementsVo)
     * */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {

        PageResult result = movementsService.findByRecommend(page, pagesize);
        return ResponseEntity.ok(result);

    }

    /*
     * 根据动态表的id查询单条动态
     *    请求方式和请求链接 :GET  /:id
     *    请求参数:  id
     *    返回值:MovementsVo
     * */

    @GetMapping("/{id}")
    public ResponseEntity findByMovementId(@PathVariable("id") String id) {

        MovementsVo vo = movementsService.findByMovementId(id);

        return ResponseEntity.ok(vo);


    }



    /*
     * 动态-点赞
     *    请求方式和请求链接 : GET  /:id/like
     *    请求参数: 动态id
     *    返回值: 	integer count 点赞数
     * */
    @GetMapping("/{movementId}/like")
    public ResponseEntity like(@PathVariable("movementId") String movementId){

        Integer count= movementsService.like(movementId);

        return ResponseEntity.ok(count);
    }

    /*
     *   动态-取消点赞
     *    请求方式和请求链接 : GET /:id/dislike
     *    请求参数: 动态id
     *    返回值:  	integer count数
     * */

    @GetMapping("/{movementId}/dislike")
    public ResponseEntity dislike(@PathVariable("movementId") String movementId){

        Integer count= movementsService.dislike(movementId);

        return ResponseEntity.ok(count);
    }


    /*
    *  动态-喜欢
    *   请求方式和请求链接 : GET/movements/{id}/love
    *   请求参数:
    *    返回值: integer 喜欢总数
    * */
    @GetMapping("/{movementId}/love")
    public ResponseEntity love(@PathVariable("movementId") String movementId) {

       Integer count= movementsService.love(movementId);

        return ResponseEntity.ok(count);
    }


    /*
     *  动态-取消喜欢
     *    请求方式和请求链接 : GET   /{movementId}/unlove
     *    请求参数:
     *    返回值: integer 喜欢总数
     * */
    @GetMapping("/{movementId}/unlove")
    public ResponseEntity unlove(@PathVariable("movementId") String movementId) {

        Integer count= movementsService.unlove(movementId);

        return ResponseEntity.ok(count);
    }
}
